package cst.a903dss.zyfypt_903dss;

import java.util.List;

import cst.a903dss.zyfypt_903dss.Bean.ArticleBean;
import cst.a903dss.zyfypt_903dss.Bean.LoginBean;
import cst.a903dss.zyfypt_903dss.Bean.ProjectBean;
import cst.a903dss.zyfypt_903dss.Bean.RegBean;
import cst.a903dss.zyfypt_903dss.Bean.SVideoBean;
import cst.a903dss.zyfypt_903dss.Bean.TcaseBean;
import cst.a903dss.zyfypt_903dss.Bean.TwareBean;
import cst.a903dss.zyfypt_903dss.Bean.VideoBean;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by 双 on 2017/5/8.
 */

public class ResModel<T> implements AmicoolIface {
    private Retrofit retrofit;
    private  String SESSION_ID="tlb40fscfkka9q4eaatkvtajk2";
    private ResService service;
    //private SharedPreferences sp;
    private static final String BASE_URL="http://amicool.neusoft.edu.cn/";

    public ResModel(String type,String s_id){
        if(type.equals("gson")){
            retrofit=new Retrofit.Builder().
                    baseUrl(BASE_URL).
                    addConverterFactory(GsonConverterFactory.
                            create()).
                    build();
        }
        else {
            retrofit=new Retrofit.Builder().
                    baseUrl(BASE_URL).
                    addConverterFactory(ScalarsConverterFactory.
                            create()).
                    build();
        }
        service=retrofit.create(ResService.class);
        SESSION_ID=s_id;
    }
    public ResModel(String type){
        if(type.equals("gson")){
            retrofit=new Retrofit.Builder().
                    baseUrl(BASE_URL).
                    addConverterFactory(GsonConverterFactory.
                            create()).
                    build();
        }
        else {
            retrofit=new Retrofit.Builder().
                    baseUrl(BASE_URL).
                    addConverterFactory(ScalarsConverterFactory.
                            create()).
                    build();
        }
        service=retrofit.create(ResService.class);
        SESSION_ID="tlb40fscfkka9q4eaatkvtajk2";
    }
    @Override
    public void login(String u, String p, final AmicoolListener l) {
        Call<LoginBean> call=service.login(u,p);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {

                l.onFail(t.toString());
            }
        });
    }

    @Override
    public void reg(String u, String p, String tel, String roleid, String email,final AmicoolListener l) {
        Call<RegBean> call=service.reg(u,p,tel,roleid,email);
        call.enqueue(new Callback<RegBean>() {
            @Override
            public void onResponse(Call<RegBean> call, Response<RegBean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<RegBean> call, Throwable t) {
                l.onFail(t.toString());
            }
        });

    }
    @Override
    public void getList(String mod, int page, final AmicoolListener l) {
     switch(mod){

         case"article":
             Call<List<ArticleBean>> call_a=service.getArticleList(mod,page,SESSION_ID);
             call_a.enqueue(new Callback<List<ArticleBean>>() {
                 @Override
                 public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                     l.onResponse(response.body());
                 }

                 @Override
                 public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                     l.onFail(t.toString());
                 }
             });
             break;
         case"tware":
             Call<List<TwareBean>> call_b=service.getTwareList(mod,page,SESSION_ID);
             call_b.enqueue(new Callback<List<TwareBean>>() {
                 @Override
                 public void onResponse(Call<List<TwareBean>> call, Response<List<TwareBean>> response) {
                     l.onResponse(response.body());
                 }

                 @Override
                 public void onFailure(Call<List<TwareBean>> call, Throwable t) {
                   l.onFail(t.toString());
                 }
             });
             break;
         case"video":
             Call<List<VideoBean>> call_c=service.getVideoList(mod,page,SESSION_ID);
             call_c.enqueue(new Callback<List<VideoBean>>() {
                 @Override
                 public void onResponse(Call<List<VideoBean>> call, Response<List<VideoBean>> response) {
                     l.onResponse(response.body());
                 }

                 @Override
                 public void onFailure(Call<List<VideoBean>> call, Throwable t) {
                     l.onFail(t.toString());
                 }
             });
             break;
         case"svideo":
             Call<List<SVideoBean>> call_d=service.getSVideoList("video",page,SESSION_ID);
             call_d.enqueue(new Callback<List<SVideoBean>>() {
                 @Override
                 public void onResponse(Call<List<SVideoBean>> call, Response<List<SVideoBean>> response) {
                     l.onResponse(response.body());
                 }

                 @Override
                 public void onFailure(Call<List<SVideoBean>> call, Throwable t) {
                        l.onFail(t.toString());
                 }
             });
             break;
         case"tcase":
             Call<List<TcaseBean>> call_t=service.getTcaseList(mod,page,SESSION_ID);
             call_t.enqueue(new Callback<List<TcaseBean>>() {
                 @Override
                 public void onResponse(Call<List<TcaseBean>> call, Response<List<TcaseBean>> response) {
                     l.onResponse(response.body());
                 }

                 @Override
                 public void onFailure(Call<List<TcaseBean>> call, Throwable t) {
                      l.onFail(t.toString());
                 }
             });
             break;
         case "project":
             Call<List<ProjectBean>> call_p=service.getProjectList(mod,page,SESSION_ID);
             call_p.enqueue(new Callback<List<ProjectBean>>() {
                 @Override
                 public void onResponse(Call<List<ProjectBean>> call, Response<List<ProjectBean>> response) {
                     l.onResponse(response.body());
                 }

                 @Override
                 public void onFailure(Call<List<ProjectBean>> call, Throwable t) {
                     l.onFail(t.toString());
                 }
             });
             break;
     }
    }

    @Override
    public void getDetail(String mod, int id, String s, AmicoolListener l) {

    }
}
